package Dao;
import Negocio.Cliente;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;




import Negocio.*;
import Persistencia.HibernateUtil;
public class ClienteDao {
	
	private static ClienteDao instancia = null;
	private static SessionFactory sf = null;
	
	public static ClienteDao getInstancia() {
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new ClienteDao();
		} 
		return instancia;
	}
	
	public void agregarCliente(Cliente pro)  {		
		
		Session session = null;
		try {
			session = sf.openSession();			
			session.beginTransaction();
					session.save(pro);			
			session.getTransaction().commit();
			session.flush();
			session.close();		
		} catch (Exception e) {
			if(session != null){
				session.getTransaction().rollback();	
				session.delete(pro.getCondicion());
				session.flush();
				session.close();
//				throw new RemoteException();
			}			
		}		
	}
	
	public Cliente buscarClienteXCuit(String cuit) {
		Session session = sf.openSession();
		session.beginTransaction();
		Query query = session.createQuery("from Cliente where cuit = ?");
		query.setString(0, cuit);
		Cliente Cliente = (Cliente)query.uniqueResult();
		session.flush();
		session.getTransaction().commit();
		session.close();
		return Cliente;
	}	

	public Cliente buscarClienteXId(int id) {
		Session session = sf.openSession();
		session.beginTransaction();
		Cliente Cliente = (Cliente)session.get(Cliente.class, id);
		session.flush();
		session.getTransaction().commit();
		session.close();
		return Cliente;
	}	
	
	public void modificarCliente(Cliente prov) throws RemoteException {
		Session session = null;
		try {
			session = sf.openSession();
			session.beginTransaction();
		
			session.flush();
			session.update(prov);
			session.flush();
			session.getTransaction().commit();
			session.close();
		} catch (Exception e) {	
			if(session != null){
				session.getTransaction().rollback();
				session.flush();
				session.close();
				throw new RemoteException();
			}	
			throw new RemoteException();
		}		
	}

	public void borrarCliente(int id) {
		Session session = sf.openSession();
		session.beginTransaction();
		Cliente Cliente = (Cliente)session.get(Cliente.class, id);
		session.delete(Cliente);
		session.flush();
		session.getTransaction().commit();
		session.close();		
	}

	public List<Cliente> obtenerClientes() {
		Session session = sf.openSession();
		session.beginTransaction();
		Query query = session.createQuery("from Cliente");		
		List<Cliente> Clientees = (List<Cliente>) query.list();
		session.flush();
		session.getTransaction().commit();
		session.close();
		return Clientees;
	}

	public Cliente buscarClienteXNombre(String Cliente) {
		Session session = sf.openSession();
		session.beginTransaction();
		Query query = session.createQuery("from Cliente where razonsocial = ?");
		query.setString(0, Cliente);
		Cliente prov=null;
		try{
			prov = (Cliente) query.list().get(0);
		}catch(NullPointerException e)
		{
			//no existe el cliente buscado
		}
//		PaisOrigen paisOrigen = (PaisOrigen)session.get(PaisOrigen.class, origen);
		session.flush();
		session.getTransaction().commit();
		session.close();
		return prov;		
	}
}
